/*
Step 1: create similarity variables between comparison pairs

Step 2: run quasi-experiment
*/






// STEP 1
clear
use matched_chambers_redbook_info

merge m:1 matched_name_id using matched_name_mapping
keep if _merge == 3
drop _merge

// tack on additional information from the redbook
merge m:1 ndcnum using "redbook", keepus(mstfmds strngth)
keep if _merge == 3
drop _merge

save chambers_drugs_w_redbook_variables, replace


clear
use chambers_pairs

rename drugname matched_name
joinby matched_name using chambers_drugs_w_redbook_variables
rename (strngth mstfmds) (strngth1 mstfmds1)
rename matched_name drugname

rename comparator matched_name
joinby matched_name using chambers_drugs_w_redbook_variables
rename (strngth mstfmds) (strngth2 mstfmds2)
rename matched_name comparator 

// create some similarity variables
gen same_form = (mstfmds1 == mstfmds2)
gen same_form_strength = (mstfmds1 == mstfmds2) & (strngth1 == strngth2)
// collapse to the comparison_id level

collapse (max) same_form same_form_strength (first) drugname comparator incremental_qaly, by(comparison_id)

tab same_form
tab same_form_strength

reg incremental_qaly same_form same_form_strength, r

save comparison_level_similarity_variables, replace








// STEP 2

clear
use user_quarter_choice_chambers


// 1) focus just on treatment and control users for each comparison
joinby enrolid using eligible_analysis_users

// 2) need to create variables indicating whether they use the entering drug in a given period
keep if matched_name_id == drug_id | matched_name_id == comparator_id


// generate variables: initial_choice, chose_entry_drug (in a given quarter), rel_quart
// a few repeats, need to deal with this in a good way
unique enrolid comparison_id quarter

rename entering_drug initial_choice // whether the person's initial choice was the entering drug for a given comparison

drop rel_quarter
gen rel_quart = quarter - entry_quarter
gen chose_entry_drug = (matched_name_id == drug_id)



// 4) show graphs
// 5) Do heterogeneity by QALY difference, priority review
merge m:1 comparison_id using "chambers_pairs", keepus(incremental_qaly max_incremental_qaly priority)
keep if _merge == 1 | _merge == 3 // some will be missing
drop _merge

merge m:1 comparison_id using "comparison_level_similarity_variables"
keep if _merge == 3
drop _merge


// Filter out non-chronic drugs
merge m:1 matched_name_id using "median_prescriptions_stats_drug_level"
keep if _merge == 3
drop _merge
// 2 is the threshold in Alpert (JHE)
keep if median_prescriptions >= 2
// END filter


// summary stats
summ incremental_qaly, detail // AVG: 0.04, SD: 0.14
unique matched_name_id
unique comparison_id

foreach v in incremental_qaly max_incremental_qaly priority same_form same_form_strength {
	
	gen initial_`v' = initial_choice * `v'
	gen treat_`v' = treat * `v'
}





forvalues i=3(3)15 {
	
	display "Offset: `i' quarters"	
	eststo: ivregress 2sls chose_entry_drug (initial_choice initial_incremental_qaly initial_same_form=treat treat_incremental_qaly treat_same_form) if rel_quart == `i', vce(cluster comparison_id)	
}

esttab using table_3.csv, con r2 se label replace keep(initial_choice initial_incremental_qaly initial_same_form
eststo clear



